<!--点名记录-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="搜索学员" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学员姓名/手机号"
          clearable
          style="width: 240px"
          @keyup.enter="getList"
        >
          <template #append>
            <el-button :icon="Search" @click="getList" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="所在班级" prop="classId">
        <el-select
          v-model="queryParams.classId"
          placeholder="请选择所在班级"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in classList"
            :key="item.id"
            :label="item.beOver === '0' ? item.name + '(已结业)' : item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="授课课程" prop="courseId">
        <el-select
          v-model="queryParams.courseId"
          placeholder="请选择授课课程"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in courseList"
            :key="item.id"
            :label="item.state === '1' ? item.name + '(已停用)' : item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="授课方式" prop="isVip">
        <el-select
          v-model="queryParams.isVip"
          placeholder="请选择授课方式"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in edu_teaching_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上课日期" prop="classDate">
        <el-date-picker
          v-model="classDate"
          type="daterange"
          range-separator="~"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="classDateRangeChange"
          value-format="YYYY-MM-DD"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="主班老师" prop="teacher">
        <el-select
          v-model="queryParams.teacher"
          placeholder="请选择主班老师"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in teacherList"
            :key="item.value"
            :label="
              !item.isJob
                ? item.label + '(已离职)'
                : !item.isTeacher
                ? item.label + '(非授课)'
                : item.label
            "
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="助教老师" prop="assistant">
        <el-select
          v-model="queryParams.assistant"
          placeholder="请选择助教老师"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in teacherList"
            :key="item.value"
            :label="
              !item.isJob
                ? item.label + '(已离职)'
                : !item.isTeacher
                ? item.label + '(非授课)'
                : item.label
            "
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="缺课状态" prop="state">
        <el-select
          v-model="queryParams.state"
          placeholder="请选择缺课状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in missedStatusList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="补课状态" prop="repairState">
        <el-select
          v-model="queryParams.repairState"
          placeholder="请选择补课状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in makeupStatusList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="getList"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="handleNew()">
          开班补课
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain icon="Download" @click="handleExport()">
          导出
        </el-button>
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" border>
      <el-table-column
        label="学员姓名"
        align="center"
        prop="studentName"
        fixed="left"
      />
      <el-table-column
        label="手机号"
        align="center"
        prop="mobile"
        width="140"
        fixed="left"
      />
      <el-table-column label="班级名称" align="center" prop="className" />
      <el-table-column label="授课课程" align="center" prop="courseNameC" />
      <el-table-column
        label="上课时间"
        align="center"
        prop="classDate"
        width="200"
      >
        <template #default="scope">
          {{
            scope.row.weekNum
              ? scope.row.classDate.slice(0, 10) +
                "(周" +
                {
                  1: "一",
                  2: "二",
                  3: "三",
                  4: "四",
                  5: "五",
                  6: "六",
                  7: "日",
                }[scope.row.weekNum] +
                ")" +
                "  " +
                scope.row.classStartTime +
                "~" +
                scope.row.classEndTime
              : scope.row.classDate.slice(0, 10) +
                "  " +
                scope.row.classStartTime +
                "~" +
                scope.row.classEndTime
          }}
        </template>
      </el-table-column>
      <el-table-column label="补课状态" align="center" prop="repairStateStr" />
      <el-table-column label="补课详情" align="center" width="100">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleDetail(scope.row)"
            v-if="scope.row.detail === 0"
          >
            查看详情
          </el-button>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="主班老师" align="center" prop="teacherName" />
      <el-table-column label="助教老师" align="center" prop="assistantName" />
      <el-table-column label="消耗方式" align="center" prop="courseName" />
      <el-table-column label="应扣额度" align="center" prop="classHour">
        <template #default="scope">
          {{ scope.row.classHour + "课时" }}
        </template>
      </el-table-column>
      <el-table-column label="实扣额度" align="center" prop="classHourPay">
        <template #default="scope">
          {{ scope.row.classHourPay + "课时" }}
        </template>
        <!-- <template #default="scope">
          <div v-if="!scope.row.ifedit2" style="display: flex">
            <div class="show-text-style">
              {{ scope.row.classHourPay + "课时" }}
            </div>
            <div class="edit-icon-style" @click="scope.row.ifedit2 = true">
              <EditPen style="width: 1em; height: 1em; margin-right: 8px" />
            </div>
          </div>
          <div v-else style="display: flex">
            <el-form-item :prop="`list.${scope.$index}.classHourPay`">
              <el-input-number
                :min="0"
                controls-position="right"
                v-model="scope.row.classHourPay"
                @change="saveClassHourPay(scope.row, $event)"
                style="width: 80px"
              ></el-input-number>
            </el-form-item>
            <div class="keshi-text-style">课时</div>

            <div class="check-icon-style" @click="scope.row.ifedit2 = false">
              <Check style="width: 1em; height: 1em; margin-right: 8px" />
            </div>
          </div>
        </template> -->
      </el-table-column>
      <el-table-column label="缺课状态" align="center" prop="state" />

      <el-table-column label="上课内容" align="center" prop="description" />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        min-width="140"
        fixed="right"
      >
        <template #default="scope">
          <div v-if="!scope.row.ifedit" style="display: flex">
            <el-tooltip
              class="box-item"
              effect="light"
              :content="scope.row.remark"
              placement="top"
            >
              <div class="show-text-style">{{ scope.row.remark }}</div>
            </el-tooltip>

            <div class="edit-icon-style" @click="scope.row.ifedit = true">
              <EditPen style="width: 1em; height: 1em; margin-right: 8px" />
            </div>
          </div>
          <div v-else style="display: flex">
            <el-form-item :prop="`list.${scope.$index}.remark`">
              <el-input
                v-model="scope.row.remark"
                @change="saveRemark(scope.row, $event)"
                width="160"
              ></el-input>
            </el-form-item>
            <div class="check-icon-style" @click="scope.row.ifedit = false">
              <Check style="width: 1em; height: 1em; margin-right: 8px" />
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleAddMakeup(scope.row)"
            v-if="scope.row.completed === 1 && scope.row.repairState === 1"
          >
            插班补课
          </el-button>
          <el-button
            link
            type="primary"
            @click="handleMakeup(scope.row)"
            v-if="scope.row.repairState === 1"
          >
            标记已补
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
      size="small"
    />
  </div>
  <JoinClass ref="joinClassRef" :item="current" @refreshPage="getList" />
  <MakeupDetail ref="makeupDetailRef" :item="current" />
  <OpenClass ref="openClassRef" @refreshPage="getList" />
</template>

<script setup>
import JoinClass from "./joinClassForMakeup.vue";
import OpenClass from "./openClassForMakeup.vue";
import MakeupDetail from "./makeupDetail.vue"; // 补课详情
import { Search } from "@element-plus/icons-vue";
import {
  getMissedListAPI,
  exportOrderListSecondAPI,
  getClassListAPI,
  updateOrderByIdAPI,
} from "@/api/registrar/class";
import { getCourseListAPI } from "@/api/registrar/course";
import { getPeopleList } from "@/api/studentManagement/index";

const { proxy } = getCurrentInstance();

const { edu_teaching_mode } = proxy.useDict("edu_teaching_mode");

const missedStatusList = [
  { value: "4", label: "请假" },
  { value: "5", label: "未到" },
];

const makeupStatusList = [
  { value: "1", label: "待补课" },
  { value: "0", label: "已补课" },
];

const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    classStartTime: null,
    classEndTime: null,
    classId: null,
    teacher: null,
    assistant: null,
    courseId: null,
    state: null,
    repairState: null,
    name: null,
    isVip: null,
  },
});

const { queryParams } = toRefs(data);

const list = ref([]);
const classList = ref([]);
const teacherList = ref([]);
const courseList = ref([]);
const classDate = ref(null);

const joinClassRef = ref(null);
const makeupDetailRef = ref(null);
const openClassRef = ref(null);
const current = ref({});

// 事件------------------------------------------------------------------------------------------------------------

function classDateRangeChange(event) {
  classDate.value = event;
  queryParams.value.classStartTime = event ? event[0] : null;
  queryParams.value.classEndTime = event ? event[1] : null;
}

// 开班补课
function handleNew() {
  openClassRef.value.openW();
}

// 导出
function handleExport() {
  let {
    classStartTime,
    classEndTime,
    callStartTime,
    callEndTime,
    classId,
    teacher,
    assistant,
    courseId,
    state,
    repairState,
    name,
    isVip,
  } = queryParams.value;
  let param = {
    classStartTime,
    classEndTime,
    callStartTime,
    callEndTime,
    classId,
    teacher,
    assistant,
    courseId,
    state,
    repairState,
    name,
    isVip,
  };
  exportOrderListSecondAPI({ ...param, exportType: "3" }).then((res) => {
    let blob = new Blob([res], {
      type: "application/vnd.ms-excel;charset=utf-8",
    });
    const url = window.URL.createObjectURL(blob),
      link = document.createElement("a");
    link.href = url;
    link.download = "缺课补课表";
    link.click();
    URL.revokeObjectURL(url); // 释放内存
    proxy.$modal.msgSuccess("导出成功");
  });
}

// 查看补课详情
function handleDetail(row) {
  current.value = row;
  makeupDetailRef.value.openW(row);
}

// 插班补课
function handleAddMakeup(row) {
  current.value = row;
  joinClassRef.value.openW();
}

// 标记已补
function handleMakeup(row) {
  updateOrderById({ id: row.id, repairState: "0" }, getList);
}

// 编辑备注
function saveRemark(row, $event) {
  updateOrderById({ id: row.id, remark: row.remark }, getList);
}

// 编辑实扣额度
function saveClassHourPay(row, $event) {
  updateOrderById({ id: row.id, classHourPay: row.classHourPay }, getList);
}

function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    classStartTime: null,
    classEndTime: null,
    callStartTime: null,
    callEndTime: null,
    classId: null,
    teacher: null,
    assistant: null,
    courseId: null,
    state: null,
    repairState: null,
    name: null,
  };
  classDate.value = null;

  getList();
}

// 方法------------------------------------------------------------------------------------------

// 课程
function getCourseList() {
  getCourseListAPI({
    pageSize: 9999,
    pageNum: 1,
    open: 1,
  }).then((response) => {
    courseList.value = response.rows;
    loading.value = false;
  });
}

// 班级
function getClassList() {
  loading.value = true;
  getClassListAPI({ pageSize: 9999, pageNum: 1 }).then((response) => {
    classList.value = response.rows;
    loading.value = false;
  });
}

// 获取教师列表
function getTeacherList() {
  loading.value = true;
  getPeopleList({ isTeach: 1 }).then((response) => {
    teacherList.value = response.data;
    loading.value = false;
  });
}

function updateOrderById(valueMap, callback) {
  loading.value = true;
  updateOrderByIdAPI(valueMap)
    .then((response) => {
      callback(response.data);
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}

function initPicker() {
  getCourseList();
  getClassList();
  getTeacherList();
}

function getList() {
  loading.value = true;
  getMissedListAPI({ ...queryParams.value }).then((response) => {
    list.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 调用-------------------------------------------------------------------------------------------------------------------
initPicker();
getList();
</script>

<style lang="scss" scoped>
.info {
  display: inline-flex;
  align-items: center;
  height: 32px;
}
.show-text-style {
  width: 178px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.keshi-text-style {
  //font-size: 20px;
  height: 32px;
  line-height: 32px;
  display: flex;
  text-align: right;
}
.edit-icon-style {
  font-size: 20px;
  text-align: right;
  cursor: pointer;
}
.check-icon-style {
  font-size: 20px;
  height: 32px;
  display: flex;
  align-items: center;
  margin-left: 10px;
  cursor: pointer;
}
</style>
